<?php
if(!isset($_GET['path'])){
	die('参数错误');
}
$errimg='static/images/no.jpg';
$filepath  = $_GET['path'];
if((empty($_GET['width']) && empty($_GET['height'])) || !file_exists($filepath)){
	echo fread(fopen($errimg,'rb'),filesize($errimg));exit;
}
$imagesize = @getimagesize($filepath);
$mime      = $imagesize['mime'];
header('Content-type: '.$mime);
$dir       = dirname($filepath);//文件的存放路径
$filename  = $filepath.'.resize_'.$_GET['width'].'_'.$_GET['height'].'.'.end(explode('.', $filepath));
if(file_exists($filename)){
	echo fread(fopen($filename,'rb'),filesize($filename));exit;
}
//===========新图参数=============//
$to_width  = isset($_GET['width'])?$_GET['width']:0;//需要生成的图片宽度
$to_height = isset($_GET['height'])?$_GET['height']:0;//需要生成的图片高度
if(empty($to_width) && empty($to_width)){
    $to_w_h = $to_width/$to_height;
}else{
    $to_w_h = 1;
}

//===========原图参数=============//
$old_width = $imagesize[0];//原图片宽度
$old_height= $imagesize[1];//原图片高度
$old_w_h   = $old_width/$old_height;

if($to_width && empty($to_height)){
	if($to_width<=$old_width){
		$width=$to_width;
		$height=$width/$old_w_h;
	}else{
		$width=$old_width;
		$height=$old_height;
	}
}elseif (empty($to_width) && $to_height){
	if($to_height<=$old_height)
	{
		$height=$to_height;
		$width=$height*$old_w_h;
	}else{
		$height=$old_height;
		$width=$old_width;
	}
}else{
	if($to_w_h>=$old_w_h)
	{//新图宽高比大于等于原图的宽高比
		if($to_width<=$old_width)
		{
			$width=$to_width;
			$height=$width/$old_w_h;
		}else{
			$width=$old_width;
			$height=$old_height;
		}
	}else
	{
		if($to_height<=$old_height)
		{
			$height=$to_height;
			$width=$height*$old_w_h;
		}else{
			$height=$old_height;
			$width=$old_width;
		}
	}
}
$width  = (int)$width;
$height = (int)$height;
include './protected/components/Libimagick.php';
$image = new Libimagick();
$image->open($filepath);
$image->resize_to($width,$height,'scale');//改变原图大小
$image->save_to($filename);
$image->output();
/* 
//获取图片信息
$im = '';
if($imagesize[2] == 1) {
	$type='gif';
	$im = imagecreatefromgif($filepath);
} elseif($imagesize[2] == 2) {
	$type='jpg';
	$im = imagecreatefromjpeg($filepath);
} elseif($imagesize[2] == 3) {
	$type='png';
	$im = imagecreatefrompng($filepath);
}
if(!$im) exit;
imagesavealpha($im,true);//这里很重要;

if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled")) {
	@$ni = imagecreatetruecolor($width, $height);
	imagealphablending($ni,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
	imagesavealpha($ni,true);//这里很重要,意思是不要丢了$thumb图像的透明色;
	imagecopyresampled($ni, $im, 0, 0, 0, 0, $width, $height, $old_width, $old_height);
} elseif(function_exists("imagecreate") && function_exists("imagecopyresized")) {
	@$ni = imagecreate($width, $height);
	imagealphablending($ni,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
	imagesavealpha($ni,true);//这里很重要,意思是不要丢了$thumb图像的透明色;
	imagecopyresized($ni, $im, 0, 0, 0, 0, $width, $height, $old_width, $old_height);
} else {
	exit;
}
if($type=='jpg'){
	imagejpeg($ni, $filename);
	imagejpeg($ni);
}elseif ($type=='png'){
	imagepng($ni, $filename);
	imagepng($ni);
}elseif ($type=='gif'){
	imagegif($ni, $filename);
	imagegif($ni);
}
imagedestroy($ni);
imagedestroy($im); */
//echo fread(fopen($filename,'rb'),filesize($filename));
?>